'use strict';
const  path =  require('path');
const  fs =  require('fs');
const util = require('../util/util') 
const Controller = require('./base');
const awaitWriteStream = require('await-stream-ready').write
const sendToWormhole = require('stream-wormhole')

/**
 * @controller Sign
 */
 class SignController extends Controller { 

  async list() { 
    this.check(); 
    const { ctx ,app} = this;  
    let id = this.ctx.state.user.data.id
    const results = await app.mysql.select('sign',{
      where: { uid: id },
      orders: [['created_at','desc'], ['id','desc']],
      limit: 10,
      offset: 0
    });
    console.log("results")
    console.log(results)
    this.success(results) 
  }
 
  async sign() {  
    console.log("sign",this.formatDate())
    let id = this.ctx.state.user.data.id 
    const record = await this.app.mysql.get('sign', {  
      uid:id, 
      created_at:this.formatDate(Date.now(),"YYYY-MM-DD"),
    }); 
    console.log(record)
    if(record)  return  this.success(record) // this.error('已签到过')  
    let data = await this.app.mysql.insert('sign',{ 
      uid:id, 
      created_at:this.formatDate(Date.now(),"YYYY-MM-DD"),// "2020-10-10 10:10:10" 
    })
    console.log(id,data)
    this.success(data)

 }
  
}
module.exports =  SignController
 
 
